#include "DAFuncTools.h"
#include "DATypes.h"
#include "DesktopAssist.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFrame>
#include <QPalette>
#include <QPainter>
#include <QApplication>
#include <QDebug>
#include <QIcon>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTimer>

DA::DA(QFrame *parent)
    : QFrame(parent)
{
    setObjectName("daobj");
    setAttribute(Qt::WA_TransparentForMouseEvents);
    setWindowFlags(windowFlags()|Qt::Tool |
                   Qt::FramelessWindowHint |
                   Qt::WindowStaysOnTopHint);
//                   Qt::WindowStaysOnBottomHint);
    setAttribute(Qt::WA_TranslucentBackground);
    resize(300,650);
    setFixedSize(300,650);
    DAFunTools::GetInst()->SetPosition(this,POSITION::Right);
    dat = new DATime();
    dat->setFixedHeight(100);

    daHoliday = new DAHoliday();
    daHoliday->setFixedHeight(60);

    daSystemInfo = new DASystemInfo();
    daSystemInfo->setFixedHeight(80);

    graphicsInfo =new DAGraphicsInfo();
    graphicsInfo->setFixedHeight(40);

    NetStatus.setParent(this);
    NetStatus.setFixedSize(14,14);
    NetStatus.setStyleSheet("border-radius:7px;background-color:rgb(255,0,0);");

    LabelYiYan.setParent(this);
    LabelYiYan.setObjectName("LabelYiYan");
    LabelYiYan.setWordWrap(true);
    LabelYiYan.setAlignment(Qt::AlignTop);
    LabelYiYan.setTextInteractionFlags(Qt::TextSelectableByMouse);
    LabelYiYan.setText(getTextFromYiYan());

    LabelYiFrom.setParent(this);
    LabelYiFrom.setObjectName("LabelYiWhere");
    LabelYiFrom.setFixedHeight(20);
    LabelYiFrom.setStyleSheet("color:rgb(255,255,255);");
    LabelYiFrom.setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    QHBoxLayout *hb = new QHBoxLayout();
    hb->addWidget(&NetStatus);
    hb->addWidget(&LabelYiFrom);

    QTimer *time1 = new QTimer(this);
    connect(time1,&QTimer::timeout,[=](){
        getTextFromYiYan();
    });
    time1->start(300000);
    QVBoxLayout *vb =new QVBoxLayout();
    vb->addWidget(dat);
    vb->addWidget(daHoliday);
    vb->addWidget(daSystemInfo);
#if HAVE_NVIDIA_ML
    vb->addWidget(graphicsInfo);
#endif
    vb->addStretch();
    vb->addWidget(&LabelYiYan);
    vb->addLayout(hb);
    setLayout(vb);


    daTray = new DATray(this);
    daTray->show();

    NetWorkStatus();
    if(DAFunTools::GetInst()->isNetOnline())
    {
        NetStatus.setStyleSheet("border-radius:7px;background-color:rgb(255,0,0);");
    }
//    printf("%s(%d)-<%s>: ",__FILE__, __LINE__, __FUNCTION__);

}

DA::~DA()
{

}

void DA::paintEvent(QPaintEvent *e)
{
    //创建绘画对象
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    painter.setBrush(QBrush(QColor(0, 0, 0, 50)));
    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 5, 5);
    QWidget::paintEvent(e);
}

bool DA::event(QEvent * event)
{
    if (QEvent::ActivationChange == event->type()) {
        if (QApplication::activeWindow() != this){
//            this->close();
        }
    }
    return QWidget::event(event);
}

QString DA::getTextFromYiYan()
{
    QString text;   
    QUrl YiYan_url("https://v1.hitokoto.cn/");
    QNetworkAccessManager TextGeter;
    QEventLoop loop;
    QNetworkRequest req=QNetworkRequest(YiYan_url);
    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, true);
    QNetworkReply *reply = TextGeter.get(req);
    //完成后退出子事件循环
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    //开启子事件循环
    loop.exec();
    if (reply->error())
    {
        text = "网络错误：" + reply->errorString();
    }
    else
    {
        QString YiYanString = reply->readAll();
        QJsonParseError JsonErr;
        QJsonDocument JsonYiYan(QJsonDocument::fromJson(YiYanString.toUtf8(),&JsonErr));
        if(JsonErr.error)
        {
            text = JsonErr.errorString();
        }
        else
        {
            QJsonObject JsonObj = JsonYiYan.object();
            text = "「 "+JsonObj.value("hitokoto").toString()+" 」";
            LabelYiFrom.setText("——"+JsonObj.value("from").toString());
        }
    }
    LabelYiYan.setText(text);
    return text;
}

void DA::NetWorkStatus()
{
    QTimer *time2 = new QTimer(this);
    connect(time2,&QTimer::timeout,[=](){

        if(DAFunTools::GetInst()->isNetOnline())
        {
           NetStatusStyle =  "border-radius:7px;background-color:rgb(0,255,0);";
        }
        else
        {
           NetStatusStyle =  "border-radius:7px;background-color:rgb(255,0,0);";
        }
        NetStatus.setStyleSheet(NetStatusStyle);
    });
    time2->start(5000);
    NetStatus.setStyleSheet("border-radius:7px;background-color:rgb(255,0,0);");
}
